package com.simplenix.nicasio.tags.util;

import com.simplenix.nicasio.def.Feature;
import com.simplenix.nicasio.def.MenuDefinition;
import com.simplenix.nicasio.def.Module;
import com.simplenix.nicasio.misc.AppException;
import com.simplenix.nicasio.misc.FileUtil;
import com.simplenix.nicasio.misc.StrUtil;
import com.simplenix.nicasio.navig.Tab;
import com.simplenix.nicasio.navig.TrayManager;
import com.simplenix.nicasio.sys.FeatureSecurity;
import com.simplenix.nicasio.sys.SystemDef;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author fronald
 */
public class LoadMenu {

	public static String getContextMenuByFeature(Feature feature) throws AppException {
		String rtn = "";
		try {
			List<Module> modules = SystemDef.getInstance().getModules().getModulesList();
			for (Module module : modules) {
				List<Tab> tabs = new ArrayList<Tab>();
				if (!module.getName().equals("nicasio")) {
					String pack = module.getBasePackage();
					Class menuClass = Class.forName(pack + ".def.Menus");
					MenuDefinition md = (MenuDefinition) menuClass.newInstance();
					tabs = md.getTabs();
				} else {
					tabs.add(new Tab("home", "Home", "Application home page", "/index.jsp", "nicasio/portal/portal_context.htm", "portal"));
				}
				for (Tab tab : tabs) {
					if (feature.getTab().equals(tab.getName())) {
						if (!StrUtil.nvl(tab.getContextPage()).equals("")) {
							rtn = "<script>\\$(document).ready(getContextMenu); function getContextMenu(){\\$.get('" + SystemDef.getInstance().getContextPath() + "/" + tab.getContextPage() + "', function(data){\\$('#sideMenuContainer').html(data)});}</script>";
							break;
						}
					}
				}
			}
		} catch (ClassNotFoundException e) {
			throw new AppException("Invalid Menu class. <basepackage>.def.Menus.class not found.", e);
		} catch (InstantiationException e) {
			throw new AppException("Invalid Menu class. This implements MenuDefinition?", e);
		} catch (IllegalAccessException e) {
			throw new AppException("Illegal access to Menu class.", e);
		}
		return rtn;
	}

	public static String getHtmlByFeature(Feature feature, HttpServletRequest request) throws AppException {
		String str = "";
		try {
			str += "<div style='align: right; text-align: right;'>" + TrayManager.getInstance().getHtml(request) + "</div>";
			str += "<div id='topmenu' class='menuTabs'>\n";
			str += "<ul>\n";
			List<Module> modules = SystemDef.getInstance().getModules().getModulesList();
			int c = 0;
			for (Module module : modules) {
				List<Tab> tabs = new ArrayList<Tab>();
				if (!module.getName().equals("nicasio")) {
					String pack = module.getBasePackage();
					Class menuClass = Class.forName(pack + ".def.Menus");
					MenuDefinition md = (MenuDefinition) menuClass.newInstance();
					tabs = md.getTabs();
				} else {
					tabs.add(new Tab("home", "Home", "Application home page", "/index.jsp", "portal"));
				}
				for (Tab tab : tabs) {
					String selected;
					if (!feature.getTab().equals(tab.getName())) {
						selected = "";
					} else {
						selected = "class='selected'";
					}
					boolean cont = true;
					List<Module> mods = SystemDef.getInstance().getModules().getModulesList();
					for (Module mod : mods) {
						if (cont == false) {
							break;
						}
						java.lang.String packName = mod.getBasePackage();
						List<java.lang.String> classes = FileUtil.getClassList(packName + ".featsecurity");
						for (java.lang.String className : classes) {
							Class cla = Class.forName(className);
							FeatureSecurity o = (FeatureSecurity) cla.newInstance();
							cont = o.haveAccess(request, module, SystemDef.getInstance().getModules().getFeaturesByModule(module).getFeatureByName(tab.getFeatureName()));
							if (cont == false) {
								break;
							}
						}
					}
					if (tab.getName().equals("home") || cont) {
						str += "<li><a href='" + SystemDef.getInstance().getContextPath() + "/" + tab.getTargetPage() + "' " + selected + ">" + tab.getLabel() + "</a></li>";
					}
					c++;
				}
			}
			str += "</ul></div>";
		} catch (ClassNotFoundException e) {
			throw new AppException("Invalid Menu class. <basepackage>.def.Menus.class not found.", e);
		} catch (InstantiationException e) {
			throw new AppException("Invalid Menu class. This implements MenuDefinition?", e);
		} catch (IllegalAccessException e) {
			throw new AppException("Illegal access to Menu class.", e);
		}
		return str;
	}

}
